//问卷调查
Jes.Vote ={
	init : function(){
		if(!Jes.userSess()) return;
		Jes.cBtn('../../jesacl/html/welcome.html');
		Jes("search").onclick=function(){Jes.Vote.query();}
		Jes("add").onclick=function(){Jes.Vote.open('op');}
		Jes("post").onclick=function(){Jes.Vote.update(1);}
		Jes("stop").onclick=function(){Jes.Vote.update(2);}
		Jes("del").onclick=function(){Jes.Vote.update(3);}
		initTabs('tabView_1',Array('问卷调查查询'),0,"100%",90,Array(false));
		this.query();
	}
	,query : function(){
		Jes.page("vote_query_sql@bs",{pageSize:10});
	}
	,update : function(status){
		var ids = Jes.val("id");
		if(ids == ""){alert("至少选择一项!");return;}
		var info = status=='1'?'发布':(status=='2')?'停止':'删除';
		if(!confirm('你确定'+info+'问卷调查吗？')) return;
		Jes.sql({ids:ids,status:status,sql:"vote_update_hql@bs"});
	}
	,open : function(type,id){
		var args= arguments;
		switch(type){
			case 'op':
					_newTab(id?'问卷调查编辑':'问卷调查新增',"voteop.html".J({id:id}));
				break;
			case 'now':
					_newTab('投票操作',"votenow.html".J({id:id}));
				break;
			case 'ret':
					_newTab('投票结果',"voteret.html".J({id:id}));
				break;
		}
	}
}

//op
Jes.VoteOp = {
	init : function(){
		if(!Jes.userSess()) return;
		var id = Jes.request('id');
		Jes('add').onclick=function(){Jes.VoteOp.save();}//save
		Jes.cBtn();
		Jes.sync(function(){
			Jes.Select.voteGroup('groupid',{voteid:id});
		});
		if(id){
			Jes.queryO('TJesVote',{param:{id:id}},function(da){
				Jes.vals(da);
				if(da.status=='0'){//未发布
					Jes.query('voteitem_query_sql@bs',{param:{id:id}},function(da){
						Jes.each(da,function(i){
							Jes.VoteOp.addItem(this);
						});
						var gids = Jes.name('groupid'),len=gids.length-1;
						Jes.each(gids,function(i){
							if(i!=len) Jes.VoteOp.showGroupItem(gids[i]);
						});
					});
				}else{
					Jes.hide('add_item');
					Jes.html('info','已发布投票,不要轻易修改标题!');
				}
			});
		}
	}
	,addItem : function(da){
		if(da){
			var a = Jes.name('title'),b=Jes.name('type'),c=Jes.name('idx'),d=Jes.name('groupid');
			a= a[a.length-1],b= b[b.length-1],c= c[c.length-1],d= d[d.length-1];
			a.value = da['title'],b.value = da['type'],c.value = da['idx'],d.value = da['groupid'];
			Jes.VoteOp.changeType(b);//类型改变
			//Jes.VoteOp.showGroupItem(d);
		}else{
			Jes.reset('form2');
		}
		var div = Jes.createEl('div','',{border:'2px solid #daedfa','width':'500px'});
		Jes.html(div,Jes.html('item_hide'));
		Jes.append(div,Jes('item'));
		/*
		var div = Jes.createEl('div','',{border:'2px solid #daedfa','width':'50%'});
		Jes.append(div,Jes('item'));
		var div1= Jes.createEl('div','','','<input type="hidden" title="id" id="id" name="id">题目:<input type="text" title="题目" id="title" name="title" size="45" notnull><span class="notnull">*</span><input type="button" id="del_item" name="del_item" value="删除题目" >');
		Jes.append(div1,div);
		var div2= Jes.createEl('div','','','问题类型:<select title="问题类型" notnull id="type" name="type" class="input"><option value="1">单选</option><option value="2">多选</option></select>排序: <input type="text" title="排序" id="idx" name="idx" size="5">');
		Jes.append(div2,div);
		var div3= Jes.createEl('div'),txt = document.createTextNode("答案分组:");
		Jes.append(txt,div3);
		var sel = Jes.createEl('select',{id:'groupid',name:'groupid',className:'input','notnull':'',title:'答案分组'});
		DWRUtil.addOptions(sel,[{ name:'--------------------------', id:''}],"id","name");
		Jes.Select.voteGroup(sel);
		Jes.append(sel,div3);
		var input = Jes.createEl('input',{id:'add_group',name:'add_group',type:'button',value:'添加分组'});
		Jes.append(input,div3);
		Jes.append(div3,div);
		*/
	}
	,showGroupItem : function(obj){
		var id = obj.value;
		var div = Jes.tag('div',obj.parentNode)[0];
		if(id==''){
			Jes.html(div,'');
			return;
		}
		Jes.query('votegroupitem_query_sql@bs',{param:{'id':id},isLoad:false},function(da){
			var html='';
			for(var i=0;i<da.length;i++){
				html+=da[i].itemname+'<br>';
			}
			//var div = Jes.createEl('div','','',html);
			Jes.html(div,html);
			//Jes.append(div,);
		});
		//判断groupid 是否有编辑删除权限
	}
	//组操作
	,opGroup : function(obj,type){
		if(type=='1'){//add
			Jes.FlyDiv.show("groupdiv",null,null,0);
			DWRUtil.removeAllRows('groupitem');
			Jes.vals({groupname:'',ispub:'',id:''},'group');
			Jes.VoteOp.addGroupItem(3);
		}else if(type=='2'){//edit
			var sel = Jes.tag('select',obj.parentNode)[0],id=sel.value;
			if(id==''){
				alert('请选择一项!');
				return;
			}
			var y = parseInt(event.y);
			DWRUtil.removeAllRows('groupitem');
			Jes.queryO('votegroup_obj_query_sql@bs',{param:{groupid:id,voteid:Jes.request('id')}},function(da){
				if(!da){
					alert('其它问卷已经使用,不能修改!');
					//Jes.FlyDiv.hide();
					return;
				}
				Jes.FlyDiv.show('groupdiv',null,null,0,y);
				Jes.vals(da,'group');
				Jes.query('votegroupitem_query_sql@bs',{param:{'id':id},isLoad:false},function(da){
					Jes.VoteOp.addGroupItem(da.length,da);
				});
			});
			
		}else{//
			var sel = Jes.tag('select',obj.parentNode)[0],id=sel.value;
			if(id==''){alert('请选择一项!');return;}
			if(!confirm('你确定此组吗？')) return;
				Jes.sql({ids:id,sql:'votegroup_delete_sql@bs'},function(da){
					if(da[0]==da[1]){
						alert('删除成功');
						var els = Jes.name('groupid'),_data;
						Jes.sync(function(){
							Jes.query('votegroup_query_sql@sel',{param:{voteid:Jes.request('id')}},function(data1){
								_data = data1;
							});
						});
						Jes.each(els,function(i){
							var el = els[i],val = el.value;
							DWRUtil.removeAllOptions(el);
							DWRUtil.addOptions(el,[{ name:'--------------------------', id:''}],"id","name");
							DWRUtil.addOptions(el,_data,"id","name");
							DWRUtil.setValue(el,val);
							if(id==val) Jes.VoteOp.showGroupItem(el);
						});
					}else{
						alert('删除失败，已有问卷使用！');
					}
			});
		}
	}
	,addGroupItem : function(num,da){
		num = num || 1;
		var taEl = Jes("groupitem"),tr,td,_n='',_idx='';
		for(var i=0;i<num;i++){
			tr = Jes.Table.newTr(taEl,null,{backgroundColor:"#ffffff"});
			td = Jes.Table.newTd(tr,null,null,"<strong>答案:</strong>");
			if(da){
				_n = da[i].itemname;
				_idx = Jes.tsfNull(da[i].idx,'0');
			}
			html='<input type="text" title="答案" id="group.itemname" name="group.itemname" value="'+_n+'" size="20" notnull><span class="notnull">*</span>'
			+' <strong>排序:</strong><input type="text" title="排序" id="group.idx" name="group.idx" value="'+_idx+'" size="5">'
			+' <img src="../../lovejes/images/op/c19.gif" title="删除" style="cursor:hand;" onclick="Jes.VoteOp.delGroupItem(this);">';
			td = Jes.Table.newTd(tr,null,null,html);
		}
	}
	,delGroupItem : function(obj){
		var taEl = Jes("groupitem");
		taEl.deleteRow(obj.parentNode.parentNode.rowIndex-1);
	}
	,saveGroupItem : function(){
		//var param = Jes.vals("form2");
		Jes.save("TJesVoteGroup",{form:'form3',prefix:'group',vType:1},function(da){
			alert('保存成功!');
			var els = Jes.name('groupid'),_data;
			Jes.sync(function(){
				Jes.query('votegroup_query_sql@sel',{param:{voteid:Jes.request('id')}},function(data1){
					_data = data1;
				});
			});
			Jes.each(els,function(i){
				var el = els[i],val = el.value;
				DWRUtil.removeAllOptions(el);
				DWRUtil.addOptions(el,[{ name:'--------------------------', id:''}],"id","name");
				DWRUtil.addOptions(el,_data,"id","name");
				DWRUtil.setValue(el,val);
				var id = Jes('group.id').value;//编辑
				if(id==val) Jes.VoteOp.showGroupItem(el);
			});
			Jes.FlyDiv.hide();
		});
	}
	,save : function(){
		var param = Jes.vals("form1");
		if(param==null) return;
		if(!param.id && !param.title){
			alert('请添加问题!');
			return;
		}
		Jes.save('TJesVote',{},'vote_query_sql@bs');
	}
	//问题类型改变,如果是填空题不需要答案组
	,changeType : function(obj){
		var el = obj.parentNode.nextSibling;
		var sel = Jes.tag('select',el)[0];
		if(obj.value=='20'){
			Jes.hide(el);
			sel.removeAttribute('notnull');
			sel.value='';
			Jes.VoteOp.showGroupItem(sel);
		}else{
			Jes.attr(sel,'notnull','');
			Jes.show(el);
		} 
	}
}

Jes.VoteNow = {
	init : function(){
		if(!Jes.userSess()) return;
		var id = Jes.request('id');
		Jes('add').onclick=function(){Jes.VoteNow.save();}//save
		Jes.cBtn();
		if(id){
			Jes.queryO('TJesVote',{param:{id:id}},function(da){
				if(da.status!='1') Jes.hide('add');
				Jes.vals(da);
			});
			Jes.query('voteitemgroupitem_query_sql@bs',{param:{voteid:id}},function(da){
				var html=[],id='',type;
				Jes.each(da,function(i){
					if(id != this.id){
						html.push('<b>题目:'+this.title+'</b><br>');
						id=this.id;
						type = this.type;
					}
					if(type=='1') html.push('<input type="radio" title="'+this.title+'" class="checkbox" id="'+id+'_'+type+'" name="'+id+'_'+type+'" value="'+this.itemid+'" rule="group"/>');
					else if(type=='2')  html.push('<input type="checkbox" title="'+this.title+'" class="checkbox" id="'+id+'_'+type+'" name="'+id+'_'+type+'" value="'+this.itemid+'" rule="group"/>');
					else html.push('<textarea title="'+this.title+'" id="'+id+'_'+type+'" name="'+id+'_'+type+'" notnull></textarea>');
					html.push(' '+Jes.tsfNull(this.itemname)+'<br>');
				});
				Jes.html('item',html);
			});
		}
	}
	//投票结果显示
	,retInit : function(){
		var id = Jes.request('id');
		if(!id) return;
		Jes.cBtn();
		if(id){
			Jes.queryO('TJesVote',{param:{id:id}});
			Jes.query('voteret_query_sql@bs',{param:{voteid:id}},function(da){
				var html=[],id='',item={};
				Jes.each(da,function(i){
					if(id != this.itemid){
						html.push('<b>题目:'+this.title+'</b><br>');
						id=this.itemid;
					}
					if(item[this.itemid]) item[this.itemid] +=parseInt(Jes.tsfNull(this.num,0));
					else item[this.itemid] =parseInt(Jes.tsfNull(this.num,0));
					if(this.type!='20'){
						html.push('<div align="left">'+this.itemname+' <span style="color:#ff0000">(投票数'+Jes.tsfNull(this.num,0)+')</span>');
						html.push('<table cellSpacing="1" cellPadding="1" width="100%" align="center" bgColor="#767676"><tbody><tr><td bgColor=#ffffff><img height="7" id="votebar" name="votebar" num="'+Jes.tsfNull(this.num,0)+'" itemid="'+id+'" src="../../jesacl/themes/default/images/vote_bar.gif" width=""></td></tr></tbody></table></div>');
					}else{
						html.push('<div align="left"><a href="javascript:Jes.open(\'voteret_content.html?itemid='+this.itemid+'\',\'600\',\'400\',\'M\');">查看简答题答案</a><span style="color:#ff0000">(投票数'+Jes.tsfNull(this.num,0)+')</span></div>');
					}
				});
				Jes.html('item',html);
				var imgs = Jes.name('votebar');
				Jes.each(imgs,function(i){
					this.width= (this.num*300/item[this.itemid]);
				}); 
			});
		}
	}
	//问答题内容查询
	,retContentInit : function(){
		//if(!Jes.userSess()) return;
		var itemid = Jes.request('itemid');
		if(itemid){
			Jes.val('itemid',itemid);
			Jes('search').onclick=function(){Jes.VoteNow.queryRetContent();}
			Jes.cBtn();
			this.queryRetContent();
		}
	}
	,queryRetContent : function(){
		Jes.page("voteretcontent_query_sql@bs",{pageSize:10,isSess:0});
	} 
	,save : function(){
		Jes.save('TJesVoteRet',{},'vote_query_sql@bs');
	}
}